<?php
namespace app\redbag\model;

use app\BaseModel;
use think\facade\Db;

class Record extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_redbag_record';


    protected $append = [

      'have_num'

    ];


    /**
     * @author chenniang
     * @DataTime: 2020-04-11 15:03
     * @功能说明:邀请人数
     */
    public function getHaveNumAttr($value,$data){

        $invitation = new Invitation();

        $count = $invitation->getCount($data['id']);

        return $count;

    }

    /**
     * @author chenniang
     * @DataTime: 2020-04-10 11:24
     * @功能说明:获取记录
     */
    public function getUserList($dis){

        $data = $this->alias('a')
                ->join('longbing_card_user b','a.user_id = b.id','left')
                ->where($dis)
                ->field('a.*,b.nickName,b.avatarUrl')
                ->group('a.id')
                ->order('a.id')
                ->paginate(10)
                ->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $f_time = datachange($v,'create_time');

                $v['create_time'] = $f_time['radar_group'].$f_time['radar_time'];

                if(empty($v['avatarUrl'])){

                    $v['avatarUrl'] = 'https://lbqnyv2.migugu.com/defaultAvatar.png';
                }

            }
        }

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-10 15:45
     * @功能说明:记录列表
     */
    public function recordList($dis){

        $data = $this->where($dis)->order('id desc')->paginate(10)->toArray();

        if(!empty($data['data'])){

            $active_model = new AdminActive();

            $staff_record = new staffRecord();

            foreach ($data['data'] as &$v){

                $v['title'] = $active_model->where(['id'=>$v['active_id']])->value('title');

                $staff_dis = [

                    'staff_id' => $v['staff_id'],

                    'active_id'=> $v['active_id']
                ];

                $staff_record = $staff_record->where($staff_dis)->find();

                $v['staff_status'] = isset($staff_record->status)? $staff_record->status:1;

                if(($v['have_num']>=$v['user_num']&&$v['status']==0)||($v['status']==0&&$v['use_condition']==0)){

                    $v['status'] = 2;
                }
            }
        }
        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-10 17:32
     * @功能说明:详情
     */
    public function recordInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];


    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-11 11:15
     * @功能说明:添加
     */
    public function recordAdd($data){

        $data['create_time'] = time();

        $data['update_time'] = time();

//        if(empty($data['use_condition'])||$data['use_condition']==0){
//
//            $data['status'] = 1;
//
//        }

        $res = $this->insert($data);

        return $res;
    }















}